/*
* $Id: ViewHousingWizard.java 275 2011-02-24 23:13:33Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import apiary.Apiary;
import apiary.common.FormHelper;
import apiary.db.HousingStore.HousingRecord;
import apiary.db.managers.DBManager;
import apiary.ui.BackScreen;
import apiary.ui.journal.ChooseForm;
import apiary.ui.journal.ChooseForm.ChooseValue;
import apiary.ui.journal.ViewForm;
import apiary.ui.journal.WizardScreen;
import java.util.Hashtable;

/**
 * Wizard of view hive
 * @author lexus
 */
public class ViewHousingWizard extends Wizard
{
    private ChooseForm familyChoose;

    /**
     * Constructor
     */
    public ViewHousingWizard() {
        super(null);
        Init();
    }

    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ViewHousingWizard(BackScreen parentForm) {
        super(parentForm);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Integer idChoose = new Integer(1);
        Apiary apiary = Apiary.getInstance();
        firstForm = new ChooseForm(apiary.getLocaleString("ChooseHousing"),
                                   idChoose, this, true);
        FormHelper.fillChooseQBHList((ChooseForm) firstForm,
                                     apiary.getLocaleString("FreeHousing"),
                                     apiary.getLocaleString("ChooseFamily"));

        Integer idChooseF = new Integer(2);
        familyChoose = new ChooseForm(apiary.getLocaleString("ChooseFamily"),
                                      idChooseF, this, true);
        familyChoose.setBackScreen(firstForm);
        FormHelper.fillFamilyList(familyChoose);

        Integer idChooseH = new Integer(3);
        ChooseForm chooseForm = new ChooseForm(
                apiary.getLocaleString("ChooseHousing"), idChooseH, this, true);
        chooseForm.setBackScreen(firstForm);
        formsNext.put(idChooseF, chooseForm);
        formsNext.put(idChoose, chooseForm);

        Integer idView = new Integer(4);
        ViewForm form = new ViewForm(apiary.getLocaleString("ViewHousing"), idView, this);
        form.setBackScreen(chooseForm);
        formsNext.put(idChooseH, form);
    }

    /**
     * @deprecated In this wizard don't use
     */
    public void save() {
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        WizardScreen form = (WizardScreen) nextForm;
        if(form.getIdForm().intValue() == 3){
            ChooseValue value = (ChooseValue) getData(new Integer(1));
            int idFamily = 0;
            if(value.id == 1) {
                value = (ChooseValue) getData(new Integer(2));
                idFamily = value.id;
            }
            FormHelper.fillHousingList((ChooseForm) form, idFamily);
        } else if(form.getIdForm().intValue() == 4) {
            ChooseValue value = (ChooseValue) getData(new Integer(3));
            setHousingData((ViewForm) form, value.id);
        }
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        if(idForm.intValue() == 1) {
            ChooseValue value = (ChooseValue) dataForm;
            if(value.id == 1) {
                Apiary apiary = Apiary.getInstance();
                apiary.Display(familyChoose);
                setData(idForm, dataForm);
                return false;
            }
        }
        return true;
    }

    /**
     * Form is filled of data of hive
     * @param viewForm form
     * @param id identificator of hive
     */
    private void setHousingData(ViewForm viewForm, int id) {
        DBManager dBManager = DBManager.getInstance();
        HousingRecord housing = dBManager.getHousing(id);
        Apiary apiary = Apiary.getInstance();
        viewForm.append(apiary.getLocaleString("NameHousing").concat(": "));
        viewForm.append(housing.name.concat("\n"));
        Hashtable types = DBManager.getInstance().getTypesHousing();
        String typeH = (String) types.get(new Integer(housing.typeHousing));
        viewForm.append(apiary.getLocaleString("TypeHousing").concat(": "));
        viewForm.append(typeH.concat("\n"));
        types = DBManager.getInstance().getTypesFrame();
        String typeF = (String) types.get(new Integer(housing.typeFrames));
        viewForm.append(apiary.getLocaleString("TypeFrame").concat(": "));
        viewForm.append(typeF.concat("\n"));
    }
}
